package flyweight

import "sync"

type tMockTicketRemainingService struct {
	mRemaining map[int]int
	mRWMutex *sync.RWMutex
}

func newMockTicketRemainingService() *tMockTicketRemainingService {
	return &tMockTicketRemainingService{
		make(map[int]int, 16),
		new(sync.RWMutex),
	}
}


func (me *tMockTicketRemainingService) Get(from string, to string) ITicketRemaining {
	ticket := MockTicketService.Get(from, to)
	if ticket == nil {
		return nil
	}
	r := newMockTicketRemaining(ticket, 0)

	me.mRWMutex.RLock()
	defer me.mRWMutex.RUnlock()
	num,ok := me.mRemaining[ticket.ID()]

	if ok {
		r.iRemaining = num
	}

	return r
}

func (me *tMockTicketRemainingService) Save(id int, num int) {
	me.mRWMutex.Lock()
	defer me.mRWMutex.Unlock()
	me.mRemaining[id] = num
}

var MockTicketRemainingService ITicketRemainingService = newMockTicketRemainingService()